package com.searching.core.common;

import com.searching.core.model.FileType;
import com.searching.core.model.Thing;

import java.io.File;

/**
 * 辅助工具类
 * 将file对象转换为Thing对象
 */


public class FileConvertThing {
    public static Thing convert(File file) {
        Thing thing = new Thing();
        thing.setName(file.getName());
        thing.setPath(file.getAbsolutePath());
        thing.setDepth(computeFileDepth(file));
        return thing;
    }
    private static int computeFileDepth(File file) {
        int depth = 0;
        String[] segments = file.getAbsolutePath().split("\\\\");
        depth = segments.length;
        return depth;
    }
    private  static FileType computeFileType(File file) {
        if(file.isDirectory()) {
            return FileType.OTHER;
        }
        String fileName = file.getName();
        int index = file.getName().lastIndexOf(".");
        if(index != -1 || index < fileName.length() - 1) {
            String extend = file.getName().substring(index + 1);
            return FileType.lookup(extend);
        }else{
            return FileType.OTHER;
        }
    }
}
